package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "5.属性管理模块")
@RestController
@RequestMapping("/attributes")
@Slf4j
public class AttributeController {

    @Autowired
    IAttributeService attributeService;

    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理【添加属性】请求");
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除属性")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiImplicitParam(name = "id",value = "被删除的属性id",required = true,dataType = "long")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        log.debug("处理根据id删除属性请求");
        attributeService.deleteById(id);
        return JsonResult.ok();
    }


    @ApiOperation("根据id修改属性信息")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/updateById")
    @ApiImplicitParam(name = "id",value = "被修改的id",required = true,dataType = "long")
    public JsonResult<Void> updateById(@PathVariable Long id, AttributeUpdateDTO attributeUpdateDTO){
        log.debug("准备处理【根据id修改属性信息】请求");
        attributeService.updateById(id,attributeUpdateDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据属性模板id获取属性列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "templateId",value = "属性模板id",required = true,dataType = "long")
    @GetMapping("/list-by-template")
    public JsonResult<List<AttributeListItemVO>> list(Long templateId){
        log.debug("开始处理【根据属性模板id】获取属性列表请求");
        List<AttributeListItemVO> list = attributeService.list(templateId);
        return JsonResult.ok(list);
    }

}
